﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class gaInfo_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //分页值page
            string strPage = Request["page"];
            int page = 1;
            try
            {
                page = int.Parse(strPage);
                if (page < 1)
                {
                    page = 1;
                }
            }
            catch (Exception ex)
            {
                page = 1;
            }
            bd(page);
        }
    }
    /// <summary>
    /// 显示数据
    /// </summary>
    /// <param name="page">页</param>
    private void bd(int page)
    {
        //得到所有的数据
        IList<gaInfoNews.News> list = new gaInfoNews().GetNews();
        //分页控件分页
        AspNetPager1.RecordCount = list.Count();
        AspNetPager1.CurrentPageIndex = page;
        list = list.OrderByDescending(x => "id ").Skip((page - 1) * AspNetPager1.PageSize).Take(AspNetPager1.PageSize).ToList();
        //绑定gaInfo新闻数据
        Repeater1.DataSource = list;
        Repeater1.DataBind();
        //绑定NewsType
        CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
        DataList1.DataSource = dal.NewsType.Where(x => string.IsNullOrEmpty(x.pid));
        DataList1.DataBind();
        //绑定NewsStep新闻重要程度
        //ddlNewsStep.DataSource = dal.NewsStep;
        ddlNewsStep.DataValueField = "value";
        ddlNewsStep.DataTextField = "name";
        ddlNewsStep.DataBind();
    }
    //当前页
    public int GetPage()
    {
        return AspNetPager1.CurrentPageIndex;
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        bd(AspNetPager1.CurrentPageIndex);
    }
    protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
    {
        bd(AspNetPager1.CurrentPageIndex);
    }
    /// <summary>
    /// 导入数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    //protected void btnImport_Click(object sender, EventArgs e)
    //{
    //    var tt = Request["select"];

    //    CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();

    //    //得到选中数据
    //    var list = hidIds.Value.Split(',');
    //    string str = hidSelectValue.Value;      //选中新闻类型
    //    bool check = false;
    //    if (list.Length > 1)
    //    {
    //        //循环添加
    //        for (int i = 0; i < list.Length; i++)
    //        {
    //            int id = Convert.ToInt32(list[i]);
    //            CMSModel.News cmsnews = new CMSModel.News();
    //            gaInfoNews.News news = new gaInfoNews().GetNewsById(id);
    //            cmsnews.ctime = news.N_cTime;
    //            cmsnews.content = news.N_content;
    //            cmsnews.title = news.N_title;
    //            cmsnews.step = int.Parse(ddlNewsStep.SelectedValue);    //重要程度
    //            if (!string.IsNullOrEmpty(str))
    //            {
    //                foreach (var c in str.Split(','))
    //                {
    //                    cmsnews.News_Type.Add(new CMSModel.News_Type() { ctime = DateTime.Now, News = cmsnews, NewsTypeId = c });
    //                }
    //            }
    //            //添加入库
    //            dal.News.AddObject(cmsnews);
    //            dal.SaveChanges();
    //            //改变gaInfo中news的状态
    //            check = new gaInfoNews().UpdateNewsCheckById(id);

    //            //else {
    //            //    ClientScript.RegisterStartupScript(GetType(), "aa", "alert('请选择新闻类型');", true);
    //            //}
    //        }
    //        if (check)
    //        {
    //            //重新绑定
    //            bd(AspNetPager1.CurrentPageIndex);
    //            ClientScript.RegisterStartupScript(GetType(), "aa", "alert('导入成功');document.location='Default.aspx';", true);
    //        }
    //    }
    //    else
    //    {
    //        ClientScript.RegisterStartupScript(GetType(), "import", "alert('没有选中任何数据');", true);
    //    }

    //}
    /// <summary>
    /// 绑定新闻类型
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        var c = (NewsType_NewsTypeSelect)e.Item.FindControl("NewsTypeSelect1");
        c.BindData(0);
    }
}